package com.cg.config;


import com.cg.service.AiCodeHelperService;
import com.cg.service.InterviewAiService;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AI编程助手服务工厂
 * 负责构建和配置AiCodeHelperService实例
 */
@Configuration
@Slf4j
public class AiCodeHelperServiceFactory {

    @Autowired
    private ChatModel myQwenChatModel;

    @Autowired
    private StreamingChatModel qwenStreamingChatModel;

    @Autowired
    private ContentRetriever contentRetriever;
    
    /**
     * 创建AI编程助手服务实例
     * @return 配置完整的AiCodeHelperService
     */
    @Bean
    public AiCodeHelperService aiCodeHelperService() {
        try {
            log.info("开始初始化AI编程助手服务...");
            
            // 会话记忆配置
            ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
            
            // 构造 AI Service
            AiCodeHelperService aiCodeHelperService = AiServices.builder(AiCodeHelperService.class)
                    // 默认总结/普通聊天：使用常规温度
                    .chatModel(myQwenChatModel)
                    .streamingChatModel(qwenStreamingChatModel)
                    .chatMemory(chatMemory)
                    .chatMemoryProvider(memoryId -> 
                            MessageWindowChatMemory.withMaxMessages(10)) // 每个会话独立存储
                    .contentRetriever(contentRetriever) // RAG 检索增强生成

                    // .toolProvider(mcpToolProvider) // MCP 工具调用，暂时禁用
                    .build();
            
            log.info("AI编程助手服务初始化成功");
            return aiCodeHelperService;
            
        } catch (Exception e) {
            log.error("AI编程助手服务初始化失败", e);
            throw new RuntimeException("AI服务初始化失败", e);
        }
    }

    /**
     * 创建面试AI服务实例
     * @return 配置完整的InterviewAiService
     */
    @Bean
    public InterviewAiService interviewAiService() {
        try {
            log.info("开始初始化面试AI服务...");
            
            // 会话记忆配置
            ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
            
            // 构造面试AI Service
            InterviewAiService interviewAiService = AiServices.builder(InterviewAiService.class)
                    // 使用面试专用的流式模型
                    .streamingChatModel(qwenStreamingChatModel)
                    .chatMemory(chatMemory)
                    .chatMemoryProvider(memoryId -> 
                            MessageWindowChatMemory.withMaxMessages(10)) // 每个会话独立存储
                    .build();
            
            log.info("面试AI服务初始化成功");
            return interviewAiService;
            
        } catch (Exception e) {
            log.error("面试AI服务初始化失败", e);
            throw new RuntimeException("面试AI服务初始化失败", e);
        }
    }
}

